package com.smart.tool.future;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author xiaoqiang
 * @Version AggregateException.java, v 0.1 2025年09月13日 00:12 xiaoqiang
 * @Description: TODO
 */
public class AggregateException extends RuntimeException {
    private final List<Throwable> innerExceptions;

    public AggregateException(String message, List<Throwable> causes) {
        super(message);
        this.innerExceptions = new ArrayList<>(causes != null ? causes : Collections.emptyList());
    }

    public List<Throwable> getInnerExceptions() {
        return Collections.unmodifiableList(innerExceptions);
    }

    @Override
    public String getMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.getMessage());
        sb.append(" (包含 ").append(innerExceptions.size()).append(" 个异常):");

        for (int i = 0; i < innerExceptions.size(); i++) {
            Throwable cause = innerExceptions.get(i);
            sb.append("\n  ").append(i + 1).append(". ")
                    .append(cause.getClass().getName()).append(": ").append(cause.getMessage());
        }

        return sb.toString();
    }
}
